package com.lashou.labelutil.dao;



import java.util.List;
import java.util.Map;

import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.lashou.labelutil.model.Deal;



public class TaobaoDAO extends JdbcDaoSupport {

	private Deal populateDealFromMap(Map<String, Object> dealMap)
	{
		Deal deal = new Deal();

		deal.setId(((Long) dealMap.get("id")).intValue());
		deal.setSite((String) dealMap.get("site"));
		deal.setCity((String) dealMap.get("city"));
		deal.setUrl((String) dealMap.get("url"));
		deal.setDescription((String) dealMap.get("description"));
		deal.setPrice((Double) dealMap.get("price"));
		deal.setOriginalPrice((Double) dealMap.get("original_price"));
		deal.setDiscount((Double) dealMap.get("discount"));
		deal.setSave((Double) dealMap.get("save"));
		deal.setBuyNumber(((Long) dealMap.get("buy_number")).intValue());
		deal.setTotalPrice((Double) dealMap.get("total_price"));
		deal.setImageURL((String) dealMap.get("image_url"));
		deal.setCurrency((String) dealMap.get("currency"));
		deal.setMarker((String) dealMap.get("marker"));
		deal.setReevaluatedBuyNumber((Double) dealMap.get("reevaluated_buy_number"));
		deal.setMd5((String) dealMap.get("md5"));
		
		//comments_number,comments_score,company_name,business_name,taobao_productsURL,company_addrees
		if(null!=dealMap.get("comments_number"))
		deal.setCommentsNumber((Integer)dealMap.get("comments_number"));
		if(null!=dealMap.get("comments_score"))
		deal.setCommentsScore((Double)dealMap.get("comments_score"));
		deal.setCompanyName((String) dealMap.get("company_name"));
		deal.setBusinessName((String) dealMap.get("business_name"));
		deal.setTaobaoProductsURL((String) dealMap.get("taobao_productsURL"));
		deal.setCompanyAddrees((String) dealMap.get("company_addrees"));
		return deal;
	}
	public int getTaobaoTotal()
	{
		final String FIND_DEALS_COUNT= "SELECT count(id) FROM deal WHERE site='taobao' AND marker='PROCESSED' and comments_number='0' order by id";
		return this.getJdbcTemplate().queryForInt(FIND_DEALS_COUNT);
	}
	
	@SuppressWarnings("unchecked")
	public Deal getDealByNaturalKey(String site)
	{
		final String FIND_DEAL_BY_URL = "SELECT * FROM deal d WHERE d.site='taobao' AND d.marker='PROCESSED' and d.comments_number='0' order by id";
		List<Map<String, Object>> dealMapList = this.getJdbcTemplate().queryForList(FIND_DEAL_BY_URL);

		if (dealMapList == null || dealMapList.size() < 1)
		{
			return null;
		}

		Map<String, Object> dealMap = dealMapList.get(0);

		Deal deal = populateDealFromMap(dealMap);

		return deal;
	}
	
	public void updateDealForTaobao(Deal deal) {
			
		
			String UPDATE_EXIST_DEAL = "UPDATE deal SET description =?,comments_number=?,comments_score=?,company_name=?,business_name=?,company_addrees=? WHERE id=?";
				this.getJdbcTemplate().update(
						UPDATE_EXIST_DEAL,
						new Object[] {deal.getDescription(),deal.getCommentsNumber(),deal.getCommentsScore(),deal.getCompanyName(),deal.getBusinessName(),deal.getCompanyAddrees(), deal.getId() });
	}
	
}
